package com.ynny.Service.Impl;

import com.ynny.Dao.HobbyDao;
import com.ynny.Entity.Hobby;
import com.ynny.Entity.R;
import com.ynny.Service.HobbyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class HobbyServiceImpl implements HobbyService {
    @Autowired
    private HobbyDao hobbyDao;

    @Override
    @Transactional
    public int addHobbyTable(String number, List<Hobby> hobbies) {
        if (hobbies == null || hobbies.isEmpty()) {
            return 0; // 处理null或空列表
        }

        for (Hobby hobby : hobbies) {
            hobby.setNumber(number);
            if (hobbyDao.addHobby(hobby) != 1) {
                throw new RuntimeException("插入爱好失败"); // 触发事务回滚
            }
        }
        return 1;
    }
}
